package utils

import (
	"fmt"
	global2 "longmen/server/config/global"
	"longmen/server/pkg/db/mysql/models/models_sports"
)

const (
	updateBatches = `UPDATE db_matches SET match_status = CASE match_id %s END WHERE match_id IN ? ;`
)

func UpdateMatches(mss []models_sports.MatchesStatus) {
	if len(mss) == 0 {
		global2.Log.Error("empty update")
		return
	}
	cases := ""
	ids := make([]int, 0)
	for _, i2 := range mss {
		cases += fmt.Sprintf(" WHEN %d THEN %d ", i2.MatchId, i2.MatchStatus)
		ids = append(ids, i2.MatchId)
	}
	sql := fmt.Sprintf(updateBatches, cases)
	exec := global2.GetX().Exec(sql, ids)
	if exec.Error != nil {
		global2.Log.Error("update error: ", exec)
	}
	global2.Log.Info("update matches rows :", exec.RowsAffected)
}
